package com.miniot.environment.controller;

import cn.hutool.core.util.StrUtil;
import com.mimniot.environment.dto.*;
import com.mimniot.environment.vo.EcRealTimeCurveVO;
import com.mimniot.environment.vo.EquipmentControlVO;
import com.mimniot.environment.vo.PhRealTimeCurveVO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import com.miniot.environment.service.VsualizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/vsualization" )
@Api(value = "可视化(后台)", tags = "可视化-后台接口")
public class VsualizationController extends BaseController {
    private final VsualizationService vsualizationService;
    /**
     * 设备情况
     * @return Result
     */
    @GetMapping("/getCondition")
    @ApiOperation(value = "设备情况")
    public R<ConditionDTO> getEquipment() {
        return R.success(vsualizationService.getCondition(),"查询成功");
    }
    /**
     * 历史告警统计
     * @return Result
     */
    @GetMapping("/getHistory")
    @ApiOperation(value = "历史告警统计")
    public R<HistoryDTO> getHistory() {
        return R.success(vsualizationService.getHistory(),"查询成功");
    }

    /**
     * 设备列表
     * @return Result
     */
    @GetMapping("/getEquipmentView")
    @ApiOperation(value = "设备列表")
    public R<EquipmentViewDTO> getEquipmentView() {
        return R.success(vsualizationService.getEquipmentView(), "查询成功");
    }
    /**
     * 设备指标数据
     * @return Result
     */
    @GetMapping("/getNorm")
    @ApiOperation(value = "数据图表")
    public R<NormDTO> getNorm() {
        //TODO NormDTO里面字段不完善
        return R.success(vsualizationService.getNorm(), "查询成功");
    }

/**
 * 数据图表
 * @return Result
 */
    @GetMapping("/getTemperature")
    @ApiOperation(value = "数据图表")
    public R<TemperatureDTO> getTemperature() {
        return R.success(vsualizationService.getTemperature(), "查询成功");
    }

    /**
     * PH实时曲线
     * @return Result
     */
    @GetMapping("/getPhRealTimeCurve")
    @ApiOperation(value = "PH实时曲线")
    public R<List<PhRealTimeCurveVO>> getRealTimeCurve() {
       List<PhRealTimeCurveVO> list = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            PhRealTimeCurveVO vo  = new PhRealTimeCurveVO();
            vo.setTime(i+""+":00");
            vo.setNumber("7");
            list.add(vo);
        }

        return R.success(list, "查询成功");
    }

    /**
     * EC实时曲线
     * @return Result
     */
    @GetMapping("/getEcRealTimeCurve")
    @ApiOperation(value = "EC实时曲线")
    public R<List<EcRealTimeCurveVO>> getEcRealTimeCurve() {
        List<EcRealTimeCurveVO> list = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            EcRealTimeCurveVO vo  = new EcRealTimeCurveVO();
            vo.setTime(i+""+":00");
            vo.setNumber("8");
            list.add(vo);
        }
        return R.success(list, "查询成功");
    }

    /**
     * 设备控制查询
     * @return Result
     */
    @GetMapping("/getEquipmentControl")
    @ApiOperation(value = "设备控制查询")
    public R<List<EquipmentControlVO>> getEquipmentControl() {
        List<EquipmentControlVO> list = new ArrayList<>();
        EquipmentControlVO vo = new EquipmentControlVO();
        vo.setPumpHouse("1期1号泵房总开关");
        vo.setState(1);
        vo.setId("100");
        vo.setLatitude("29.89329");
        vo.setLongitude("106.714206");
        EquipmentControlVO.water water = new EquipmentControlVO.water();
        EquipmentControlVO.water water1 = new EquipmentControlVO.water();
        ArrayList<EquipmentControlVO.water> list1 = new ArrayList<>();
        water.setId("1");
        water.setState(1);
        water.setWaterPumpName("水泵控制#1");
        EquipmentControlVO vo1 = new EquipmentControlVO();
        ArrayList<EquipmentControlVO.water> list2 = new ArrayList<>();
        vo1.setPumpHouse("2期1号泵房总开关");
        vo1.setState(1);
        vo1.setId("10011");
        vo1.setLatitude("29.89230");
        vo1.setLongitude("106.715817");

        water1.setId("2");
        water1.setState(0);
        water1.setWaterPumpName("水泵控制#2");
        list1.add(water);
        list1.add(water1);
        list2.add(water);
        list2.add(water1);
        vo1.setWaters(list2);
        vo.setWaters(list1);

        list.add(vo);
        list.add(vo1);
        return R.success(list, "查询成功");
    }

    /**
     * 设备控制修改状态
     * @return Result
     */
    @GetMapping("/editEquipmentControl")
    @ApiOperation(value = "设备控制修改状态")
    public R editEquipmentControl(@ApiParam(value = "多个id用英文逗号分隔", required = true) @RequestParam("ids") String ids, String state) {
        System.out.println(ids   + state);
        String[] split = ids.split(",");
        if (split.length < 1) {
            return R.fail("id不能为空");
        }
        List<String> idList = new ArrayList<>();
        Arrays.stream(split).forEach(s -> {
            if (StrUtil.isNotBlank(s)) {
                idList.add(s);
            }
        });
        return R.success(true, "成功");
    }

}
